//
//  SearchTitleView.m
//  CarLifes
//
//  Created by yu on 2017/6/26.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "SearchTitleView.h"

@interface SearchTitleView ()

@end

@implementation SearchTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        if (@available(iOS 13.0, *)) {
            self.backgroundColor = UIColor.secondarySystemBackgroundColor;
        } else {
            self.backgroundColor = kBackGroundLightGrayColor;
        }
        
        UIImageView *imgView = [[UIImageView alloc] init];
//        imgView.backgroundColor = [UIColor CLTextLightGrayColor];
        imgView.image = IMAGE_NAMED(@"home_search");
        [self addSubview:imgView];
        
        self.textField = [[UITextField alloc] init];
        self.textField.font = SYSTEM_FONT(14);
        self.textField.delegate = self;
        self.textField.returnKeyType = UIReturnKeySearch;
        
        if (@available(iOS 13.0, *)) {
            UIColor *textColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
                if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                    return kTextBlackColor;
                } else {
                    return kDarkTextColor;
                }
            }];
            self.textField.textColor = textColor;
        } else {
            self.textField.textColor = kTextBlackColor;
        }
        
        self.textField.backgroundColor = [UIColor clearColor];
        self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索车主名、车架号、设备号、合同号" attributes:@{NSForegroundColorAttributeName:kTextGrayColor,NSFontAttributeName:SYSTEM_FONT(13)}];
        [self.textField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:self.textField];
        
        
        imgView.sd_layout
        .leftSpaceToView(self, 8)
        .centerYEqualToView(self)
        .heightIs(15)
        .widthEqualToHeight();
        
        self.textField.sd_layout
        .leftSpaceToView(imgView, 5)
        .rightSpaceToView(self, 0)
        .centerYEqualToView(self)
        .heightIs(30);
        
    }
    return self;
}

- (void)textFieldDidChanged:(UITextField *)textField
{
//    UITextRange *range = textField.markedTextRange;
//    NSString *text = [textField textInRange:range];
    PPLog(@"text000 = %@",textField.text);
    PPLog(@"textLength000 = %ld",textField.text.length);
//    if(text.length > 0 || textField.text.length == 0) {
//        return;
//    }

    if (self.textFieldBlock) {
        self.textFieldBlock(textField.text);
    }
}


#pragma  mark - textField delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    PPLog(@"222did changed:%@",textField.text);
    UITextRange *selectedRange = textField.markedTextRange;
    
    if(selectedRange == nil || selectedRange.empty)
    {
        [self.textField resignFirstResponder];
        
        if (self.textFieldBlock) {
            self.textFieldBlock(textField.text);
        }
    }

    return YES;
}



@end
